﻿package com.lz.football.module.guide
{
    import com.common.utils.DisplayObjectUtil;
    import com.common.utils.Reflection;
    import com.global.GlobalDef;
    import com.greensock.TweenLite;
    import com.liangzhong.events.AppEventDispatcher;
    import com.liangzhong.events.ModuleEvent;
    import com.liangzhong.events.NotifierEvent;
    import com.liangzhong.model.AppData;
    
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.Dictionary;

	/**
	 *引导基类 
	 * @author lake
	 * 
	 */	
    public class Guide extends EventDispatcher implements IGuide
    {
		/**
		 *显示界面皮肤 
		 */		
		public var _symbolSkin		:MovieClip;
		/**
		 *箭头 
		 */		
		public var arrowMc			:MovieClip;
		/**
		 *箭头角度 
		 */		
		public var arrowAngle		:int;
		/**
		 *前一引导 
		 */		
		public var parentGuide		:IGuide;
		/**
		 *下一引导 
		 */		
		public var childGuide		:IGuide;
		/**
		 *任务信息 
		 */		
		public var taskObj			:Object;
		/**
		 *引导主界面 
		 */		
		public var guideView 		:*;
		/**
		 *蒙板 
		 */		
		public var markSp			:Sprite;
		/**
		 *当前点击区域 
		 */		
		public var curClickRect		:Rectangle;
		/**
		 *索引 
		 */		
		public var index			:int = 1;
		/**
		 *步骤 
		 */		
		public var steps			:Array;
		/**
		 *当前步骤信息 
		 */		
		public var stepItem 		:XML;
		/**
		 *背景蒙板 
		 */		
		public var bgMark			:Sprite;
		/**
		 *目标点击区域 
		 */		
		public var tarClickRect		:Rectangle;
		/**
		 *蒙板透明度 
		 */		
		public var markAlpha		:Number = 0;
		/**
		 *对话框按钮 
		 */		
		public var dialogBtn		:SimpleButton;
		/**
		 *引导容器 
		 */		
		public var guideContainer 	:Sprite;
		/**
		 *场景容器 
		 */		
		public var sceneContainer	  :Sprite;
		/**
		 *场景按钮 
		 */		
		public var sceneBtn 		:DisplayObject;
		/**
		 *是否已经提交 
		 */		
		public var isPost			:Boolean = false;
		/**
		 *对话框皮肤 
		 */		
		protected var dialogSkin 	:MovieClip;

        public function Guide()
        {
			steps = [];
			bgMark = new Sprite();
			bgMark.graphics.beginFill(0x000000,0);
			bgMark.graphics.drawRect(0,0,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageHeight);
			bgMark.graphics.endFill();
			GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler,false,-5);
        }
		/**
		 *舞台大小改变 
		 * @param e
		 * 
		 */		
		protected function onResizeHandler(e :Event):void
		{
			if(bgMark)
			{
				bgMark.width = GlobalDef.GameStage.stageWidth;
				bgMark.height = GlobalDef.GameStage.stageHeight;
			}
			if(_symbolSkin)
			{
				_symbolSkin.x = (GlobalDef.GameStage.stageWidth - _symbolSkin.width)/2;
				_symbolSkin.y = (GlobalDef.GameStage.stageHeight - _symbolSkin.height)/2;
			}
			if(arrowMc && arrowMc.parent && sceneBtn)
			{
				//限制区域
				var rectPoint :Point = sceneBtn.localToGlobal(new Point(0,0));
				curClickRect = new Rectangle(rectPoint.x,rectPoint.y,sceneBtn.width,sceneBtn.height);
				step();
				//指向球场
				var arrrowPoint :Point = new Point(curClickRect.x + curClickRect.width/2,curClickRect.y - 5);
				if(arrowAngle == 90)
				{
					arrrowPoint = new Point(curClickRect.x + curClickRect.width,curClickRect.y + curClickRect.height/2);
				}
				else if(arrowAngle == -90)
				{
					arrrowPoint = new Point(curClickRect.x,curClickRect.y + curClickRect.height/2);
				}
				else if(arrowAngle == 180 || arrowAngle == -180)
				{
					arrrowPoint = new Point(curClickRect.x + curClickRect.width/2,curClickRect.y + curClickRect.height);
				}
				addArrowToScenePure(arrrowPoint.x,arrrowPoint.y,arrowAngle);
			}
			if(markSp && markSp.parent)
			{
				removeMarkSp();
				markSp = DisplayObjectUtil.getRectMarkSprite(curClickRect.x,curClickRect.y,curClickRect.width,curClickRect.height,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageHeight,0x000000,markAlpha);
				GlobalDef.AlertContainer.addChild(markSp);
			}
		}
		/**
		 *点击事件 
		 * @param e
		 * 
		 */		
		protected function clickHandler(e :MouseEvent):void
		{
			
		}
		/**
		 *初始化 
		 * 
		 */		
		protected function initView():void
		{
			arrowMc = Reflection.createInstance("com.lz.guide.res.arrowMc");
			if(arrowMc)
			{
				arrowMc.mouseChildren = false;
				arrowMc.mouseEnabled = false;
			}
		}
		/**
		 *获取任务信息 
		 * @return 
		 * 
		 */		
        public function getTask() : Object
        {
            return taskObj;
        }
		/**
		 *设置任务信息 
		 * @return 
		 * 
		 */
        public function setTask(value:Object) : void
        {
            taskObj = value;
        }
		/**
		 *执行 
		 * 
		 */
		public function process():void
		{
			
		}
		/**
		 *设置父节点 
		 * @param value
		 * 
		 */
        public function setParent(value:IGuide) : void
        {
            parentGuide = value;
        }
		/**
		 *设置子节点 
		 * @param value
		 * 
		 */		
		public function setChild(value:IGuide) : void
		{
			childGuide = value;
		}
		/**
		 *获取引导名字 
		 * @return 
		 * 
		 */
        public function getGuideName() : String
        {
            return "";
        }
		/**
		 * 对话框按钮点击
		 * @param e
		 * 
		 */		
		protected function dialogBtnClickHandler(e :MouseEvent):void
		{
			
		}
		/**
		 * 添加元件到舞台 
		 * @param disp
		 */		
		protected function addDispToStage(disp:DisplayObject):void
		{
			GlobalDef.AlertContainer.addChild(disp);
			disp.x = (GlobalDef.GameStage.stageWidth - disp.width)/2;
			disp.y = (GlobalDef.GameStage.stageHeight - disp.height)/2;
		}
		/**
		 *执行一步 
		 * 
		 */		
		public function step():void
		{
			removeMarkSp();
			markSp = DisplayObjectUtil.getRectMarkSprite(curClickRect.x,curClickRect.y,curClickRect.width,curClickRect.height,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageHeight,0x000000,markAlpha);
			GlobalDef.AlertContainer.addChild(markSp);
		}
		/**
		 *跳到某一步 目前引导比赛用 
		 * @param type
		 * 
		 */		
		public function gotoStep(type :int):void
		{
			
		}
		/**
		 *添加按钮到场景 
		 * 
		 */		
		protected function addBtnToScene(sx:Number,sy:Number):void
		{
			
		}
		/**
		 * 添加箭头到场景
		 * @param sx 坐标
		 * @param sy 坐标
		 * @param rot 旋转角度
		 * 
		 */		
		protected function addArrowToScene(sx:Number,sy:Number,rot :Number = 0):void
		{
			sceneBtn.addEventListener(MouseEvent.CLICK,onClickSceneBtn);
			if(arrowMc)
			{
				GlobalDef.GameContainer.addChild(arrowMc);
				arrowMc.rotation = rot;
				arrowMc.x = sx;
				arrowMc.y = sy;
			}
		}
		/**
		 *纯添加 
		 * @param sx
		 * @param sy
		 * @param rot
		 * 
		 */		
		protected function addArrowToScenePure(sx:Number,sy:Number,rot :Number = 0):void
		{
			if(arrowMc)
			{
				GlobalDef.GameStage.addChild(arrowMc);
				arrowMc.rotation = rot;
				arrowMc.x = sx;
				arrowMc.y = sy;
			}
		}
		/**
		 *点击场景按钮 
		 * @param e
		 * 
		 */		
		protected function onClickSceneBtn(e :MouseEvent):void
		{
			
		}
		/**
		 *消失完毕 
		 * @param disp
		 * 
		 */		
		protected function fadeOutComplete(disp :DisplayObject):void
		{
			if(disp && disp.parent)
			{
				disp.parent.removeChild(disp);
			}
		}
		/**
		 *删除遮挡 
		 * 
		 */		
		protected function removeMarkSp():void
		{
			if(markSp && markSp.parent)
			{
				markSp.parent.removeChild(markSp);
			}
		}
		/**
		 *删除背景遮挡 
		 * 
		 */		
		protected function removeBgMark():void
		{
			if(bgMark && bgMark.parent)
			{
				bgMark.parent.removeChild(bgMark);
			}
		}
		/**
		 *删除箭头 
		 * 
		 */		
		protected function removeArrowMc():void
		{
			if(arrowMc && arrowMc.parent)
			{
				arrowMc.parent.removeChild(arrowMc);
			}
		}
		/**
		 *清除ui 
		 * 
		 */		
		public function clearUI():void
		{
			removeArrowMc();
			removeMarkSp();
			removeBgMark();
			if(guideView && guideView.showFlagView && guideView.showFlagView.parent)
			{
				guideView.showFlagView.parent.removeChild(guideView.showFlagView);
			}
			if(_symbolSkin && _symbolSkin.parent)
			{
				_symbolSkin.parent.removeChild(_symbolSkin);
			}
			if(dialogSkin && dialogSkin.parent)
			{
				dialogSkin.parent.removeChild(dialogSkin);
			}
		}
		/**
		 *重置界面 中间断线进来后 重置该步之前的UI布局 
		 */		
		public function resetUI():void
		{
			
		}
		/**
		 *添加模块 事件
		 *  非强制引导加
		 */		
		public function addModuleEvent():void
		{
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.MODULE_LOAD_RES_COMPLETE,onLoadMResComplete);
			GlobalDef.GameStage.addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
		}
		/**
		 *模块删除 
		 * @param e
		 * 
		 */		
		private function onRemoveModuleHandler(e :*):void
		{
			if(arrowMc && arrowMc.parent)
			{
				arrowMc.visible = true;
			}
		}
		/**
		 * 加载模块完毕
		 * 
		 */		
		private function onLoadMResComplete(e :*):void
		{
			if(arrowMc && arrowMc.parent)
			{
				arrowMc.visible = false;
			}
		}
		/**
		 *移除模块事件 
		 * 
		 */		
		public function removeModuleEvent():void
		{
			GlobalDef.GameStage.removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			AppEventDispatcher.getInstance().removeEventListener(NotifierEvent.MODULE_LOAD_RES_COMPLETE,onLoadMResComplete);
		}
		/**
		 *更新 
		 * @param e
		 * @return 
		 * 
		 */		
		protected function onUpdateHandler(e :*):void
		{
			//更新任务
//			AppData.getInstance().taskManager.updateGuideTask();
		}
		/**
		 *清理 
		 * 
		 */		
		public function clear() : void
		{
			removeModuleEvent();
			GlobalDef.GameContainer.removeEventListener(MouseEvent.CLICK,clickHandler);
			GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			clearUI();
			dialogBtn = null;
			guideContainer = null;
			sceneContainer = null;
			sceneBtn = null;
			_symbolSkin = null;
			guideView = null;
			arrowMc = null;
			parentGuide = null;
			childGuide = null;
			markSp = null;
			bgMark = null;
		}
    }
}
